<?php

namespace Biz\service\springHomeDecActivity;

use Biz\cache\lottery\CActivityContent;
use Biz\model\springHomeDecActivity\ActivityContent as mActivityContent;
use Biz\model\springHomeDecActivity\ContentTopic as mContentTopic;
use Biz\pockets\constInterface\SpringHomeDecConfig;
use Doraemon\pockets\constInterface\FormatScene;
use Doraemon\service\Format\Article\HandleArticle;
use Doraemon\service\Format\Note\HandleNote;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\IdParse;
use Doraemon\model\Photos as mPhotos;

class ContentService implements SpringHomeDecConfig
{

    /**
     * 获取内容主题
     * @return array[]
     */
    public static function getTopic($uid = 0, $page = 1, $pageSize = 100)
    {
        $topics = [
            'main_topic' => [],
            'content_topic' => [],
            'is_over' => 0,
        ];
        $date = Util::getContentShowDate();
        $mainTopic = mContentTopic::getMainTopic($date);
        if (empty($mainTopic)) {
            return $topics;
        }
        $topics['main_topic'] = $mainTopic;
        $totalCount = mContentTopic::topicTotalCount($mainTopic['topic_id'], $date);
        $limit = [($page - 1) * $pageSize, $pageSize];
        $contentTopics = mContentTopic::getContentTopic($mainTopic['topic_id'], $date, $limit);
        foreach ($contentTopics as $key => $contentTopic) {
            $contentTopics[$key]['list'] = self::getTopicContents($uid, $contentTopic['topic_id'], $date);
        }
        shuffle($contentTopics);
        $topics['content_topic'] = $contentTopics;
        $topics['is_over'] = $page * $pageSize >= $totalCount ? 1 : 0;
        return $topics;
    }


    public static function getTopicContents($uid, $topicId, $date)
    {
        $topicContents = [];
        $cacheData = CActivityContent::getTopics($date);
        if ($cacheData) {
            $cacheDataArr = json_decode($cacheData[$topicId], true);
            if ($cacheDataArr && is_array($cacheDataArr)) {
                return $cacheDataArr;
            }
        }
        if (!$topicId) {
            return $topicContents;
        }
        $contents = mActivityContent::getContents($topicId, $date);
        if (empty($contents)) {
            return $topicContents;
        }
        $noteIds = [];
        $articleIds = [];
        $objIds = [];
        $objImages = [];
        foreach ($contents as $content) {
            if ($content['obj_type'] == mActivityContent::TYPE_NOTE) {
                $noteIds[] = $content['obj_id'];
                $objIds[] = $content['obj_id'];
            } elseif ($content['obj_type'] == mActivityContent::TYPE_ARTICLE) {
                $articleIds[] = $content['obj_id'];
                $objIds[] = $content['obj_id'];
            }
            if (isset($content['image']) && !empty($content['image'])) {
                $objImages[$content['obj_id']] = $content['image'];
            }
        }

        $notes = [];
        if (!empty($noteIds)) {
            $notes = HandleNote::handleNote($noteIds, FormatScene::MASONRY_LAYOUTS, $uid);
        }
        $articles = [];
        if (!empty($articleIds)) {
            $articles = HandleArticle::handleArticle($articleIds, FormatScene::MASONRY_LAYOUTS, $uid);
        }
        $topicContents = $articles + $notes;

        if (empty($topicContents)) {
            return $topicContents;
        }

        $objIdIndexs = array_flip(array_unique($objIds));

        uksort($topicContents, function ($a, $b) use ($objIdIndexs) {
            return $objIdIndexs[$a] < $objIdIndexs[$b] ? -1 : 1;
        });

        foreach ($topicContents as $objId => $topicContent) {
            if (isset($objImages[$objId])) {
                if ($topicContent['photo']['photo_info'] && $topicContent['photo']['photo_info']['show_type'] == mPhotos::SHOW_TYPE_PHOTO) {
                    $topicContents[$objId]['photo']['photo_info']['image_list'][0]['o_500_url'] = $objImages[$objId];
                }
                if ($topicContent['photo']['photo_info'] && $topicContent['photo']['photo_info']['show_type'] == mPhotos::SHOW_TYPE_VIDEO) {
                    $topicContents[$objId]['photo']['photo_info']['video_info']['o_500_url'] = $objImages[$objId];
                }
            }
            $topicContents[$objId]['statSign'] = basicStatSign::genStatSign(basicStatSign::ACTIVITY_SPRING_HOME_DEC_CONTENT_2022,
                [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $objId,
                    basicStatSign::EXTEND_KEY_OBJ_TYPE => IdParse::getType($objId),
                    basicStatSign::EXTEND_KEY_OBJ_UID => IdParse::getUid($objId),
                ]
            );
        }

        $topicContents = array_values($topicContents);

        CActivityContent::cacheTopics($date, [$topicId => json_encode($topicContents)]);

        return $topicContents;
    }

}